package controller;

/*
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
*/
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Component
public class ViewsInterceptor extends HandlerInterceptorAdapter {
	
	//static final Logger logger = LogManager.getLogger(IndexController.class.getName());
	final static Logger LOGGER = Logger.getLogger(BusinessController1.class.getName()); 
	
	private Object View1;
	private Object View2;
	
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		long startTime = System.currentTimeMillis();
		request.setAttribute("startTime", startTime);
		response.sendRedirect("http://host.com/outsideOfficeHours.html");
		return true;
	}

	//after the handler is executed
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		LOGGER.info("Interceptor");
		long startTime = (Long)request.getAttribute("startTime");
		long endTime = System.currentTimeMillis();
		long executeTime = endTime - startTime;
		modelAndView.addObject("executeTime",executeTime);
		/*
		if(logger.isDebugEnabled()){
			logger.debug("[" + handler + "] executeTime : " + executeTime + "ms");
		}
		*/
	}
	
	public Object getView1() {
		return View1;
	}

	public void setView1(Object view1) {
		View1 = view1;
	}

	public Object getView2() {
		return View2;
	}

	public void setView2(Object view2) {
		View2 = view2;
	}
}
